#include "step.h"
#include "step_definition.h"
#include "table.h"

using namespace CppStory;

Step::Step(const char* f, const int l)
: step_def(NULL)
, table(NULL)
, file(f)
, line(l)
, replaced(false)
{
}

Step::~Step()
{
    if (NULL != table)
        delete table;
}

void Step::set_name(std::string n)
{
    name = n;
}

const char* Step::get_name()
{
    if (replaced)
        return replace_str.c_str();
    else
        return name.c_str();
}

void Step::set_step_def(StepDefinition *s)
{
    step_def = s;
}

void Step::create_table()
{
    if (NULL == table)
        table = new Table();
}

bool Step::replace(std::string s, std::string d)
{
    std::string temp = "<" + s + ">";
    size_t pos = name.find(temp.c_str());
    if (pos == std::string::npos)
        return false;

    replace_str = name;
    replace_str.replace(pos, temp.length(), d);
    replaced = true;
    return true;
}

bool Step::run()
{
    if (NULL != get_step_def())
    {
        return get_step_def()->run(this);
    }
    return false;
}

Given::Given(const char* f, const int l)
: Step(f, l)
{
    type = "Given";
}

When::When(const char* f, const int l)
: Step(f, l)
{
    type = "When";
}

Then::Then(const char* f, const int l)
: Step(f, l)
{
    type = "Then";
}

And::And(const char* f, const int l)
: Step(f, l)
{
    type = "And";
}

But::But(const char* f, const int l)
: Step(f, l)
{
    type = "But";
}

